Page({
    data: {
      list: [],
      defaultAddressId: 0,
      radio: null,
      currentAddressId: 0
    },
  
    // 点击地址项
    onItemTap(e) {
      const { id, index } = e.currentTarget.dataset;
      this.setData({ radio: index });
      this._passAddressIdToSettle(id);
    },
  
    // 点击单选框
    onChange(event) {
      const selectedIndex = event.detail;
      const selectedId = this.data.list[selectedIndex].id;
      this.setData({ radio: selectedIndex });
      this._passAddressIdToSettle(selectedId);
    },
  
    // 私有方法：传递地址ID到结算页
    _passAddressIdToSettle(addressId) {
      const addressIdNum = parseInt(addressId);
      
      // 方法1：使用页面栈直接传递
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 2]; // 上一个页面就是结算页
      
      if (prevPage && prevPage.route.includes('settle')) {
        prevPage.setData({ 
          addressId: addressIdNum,
          hasValidAddress: true 
        });
        
        // 立即更新结算数据
        const productIds = prevPage.data.productIds.split(',').map(id => parseInt(id));
        prevPage.getCheckoutData(productIds);
      } else {
        // 方法2：使用缓存作为备用方案
        wx.setStorageSync('selectedAddressId', addressIdNum);
      }
      
      wx.navigateBack({ delta: 1 });
    },
  
    // 获取地址列表
    getAddressList() {
      const token = wx.getStorageSync('token');
      wx.request({
        url: 'http://admin.bsmall.byesame.com/wx/address/list',
        method: 'GET',
        header: { 'x-byesame-token': token },
        success: (res) => {
          if (res.data.errno === 0) {
            const list = res.data.data.list || [];
            let selectedIndex = -1;
            let defaultAddressId = 0;
  
            // 查找默认地址
            const defaultAddress = list.find(item => item.isDefault === 1);
            if (defaultAddress) {
              defaultAddressId = defaultAddress.id;
            }
  
            // 设置选中项：优先使用传入的当前地址ID，其次使用默认地址
            if (this.data.currentAddressId && this.data.currentAddressId > 0) {
              selectedIndex = list.findIndex(item => item.id === this.data.currentAddressId);
            }
            
            if (selectedIndex === -1 && defaultAddressId > 0) {
              selectedIndex = list.findIndex(item => item.id === defaultAddressId);
            }
  
            this.setData({ 
              list, 
              radio: selectedIndex,
              defaultAddressId 
            });
          } else {
            wx.showToast({ title: '获取地址列表失败', icon: 'error' });
          }
        },
        fail: (err) => {
          console.error('获取地址列表失败:', err);
          wx.showToast({ title: '网络错误', icon: 'error' });
        }
      });
    },
    AddressEdit() {
        console.log(111);
        wx.navigateTo({
            url: '../addAddress/addAddress',
        })
    },
    onLoad(options) {
      // 获取传入的当前地址ID
      if (options.currentAddressId) {
        this.setData({
          currentAddressId: parseInt(options.currentAddressId)
        });
      }
      this.getAddressList();
    },
  });111